﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ENG.EngStartQueueLib;

namespace ENG.EngStartQueue
{
  public partial class CtrExecInfo : UserControl
  {
    Bindings.Binder binder = new EngStartQueue.Bindings.Binder();
    private bool isInUpdate = false;

    public CtrExecInfo()
    {
      InitializeComponent();
    }

    ExecInfo info = null;
    private void CtrExecInfo_Load(object sender, EventArgs e)
    {
      binder.Bind("FileName", "lblFileName", "Text", EngStartQueue.Bindings.Binder.BindInfo.eDirection.FromObject); 
      binder.Bind("FullPath", "flbSource","FileName");
      binder.Bind("IsActive", "chkActive", "Checked");
      binder.Bind("Arguments", "txtArguments", "Text");
      binder.Bind("WorkingDirectory", "txtWorkingDir", "Text");
      binder.Bind("Arguments", "txtArguments", "Text");
      //binder.Bind("Priority", "cmbPriority", "SelectedItem");
      binder.Bind("Arguments", "txtArguments", "Text");
      //binder.Bind("WindowStyle", "cmbWindowStyle","SelectedItem");
      binder.Bind("ExplicitDelay","nudDelay","Value");
      binder.Bind("Source", "txtSource", "Text", ENG.EngStartQueue.Bindings.Binder.BindInfo.eDirection.FromObject);
    }

    public ExecInfo Value
    {
      set
      {
        isInUpdate = true;

        if (value == null) return;
        binder.Update(value, this);
        cmbPriority.SelectedItem = value.Priority.ToString();
        cmbWindowStyle.SelectedItem = value.WindowStyle.ToString();
        info = value;

        isInUpdate = false;
      }
      get
      {
        if (this.DesignMode) return null;

        binder.Update(this, info);
        info.Priority = 
          (System.Diagnostics.ProcessPriorityClass)
          Enum.Parse(typeof(System.Diagnostics.ProcessPriorityClass), (string) cmbPriority.SelectedItem);
        info.WindowStyle =
          (System.Diagnostics.ProcessWindowStyle)
          Enum.Parse(typeof(System.Diagnostics.ProcessWindowStyle), (string)cmbWindowStyle.SelectedItem);
        return info;
      }
    }

    private int GetEnumValue(Enum enumType, string value)
    {
      return 0;
    }


    private void flbSource_FileNameChanged(object sender, EventArgs e)
    {
      if (isInUpdate) return;

      string pom = System.IO.Path.GetDirectoryName(flbSource.FileName);
      if (pom != txtWorkingDir.Text)
        txtWorkingDir.Text = pom;

      lblFileName.Text = flbSource.RelativePath;
    }

    private void txtWorkingDir_TextChanged(object sender, EventArgs e)
    {
      if (isInUpdate) return;

      if (this.flbSource.RelativePath != txtWorkingDir.Text)
        flbSource.RelativePath = txtWorkingDir.Text;
    }
    
  }
}
